var path           = require("path");
var express        = require("express");
var jwt            = require('express-jwt');
var bodyParser     = require('body-parser');
var log4js         = require('log4js');
var tokenManager   = require('./config/token_manager');
var secret         = require('./config/secret');
var logjs          = require('./config/log4js');

//Routes
var routes = {};
routes.Busers    = require('./route/back/user.js');
routes.Bactivity = require('./route/back/Bactivity.js');
routes.Bbalance  = require('./route/back/balance.js');

routes.Ausers    = require('./route/front/user.js');
routes.Aactivity = require('./route/front/Aactivity.js');
routes.Aorder    = require('./route/front/order.js');
routes.Abalance  = require('./route/front/balance.js');
routes.Ame       = require('./route/front/me.js');



routes.property  = require('./route/property.js');






var app = express();
var serverPort = process.env.PORT || 3200;
app.listen(serverPort, "0.0.0.0", function (err) { // 192.168.7.148
  if (err) {
    console.log(err);
    return;
  }
  console.log("Listening at http://localhost:" + serverPort);
});

log4js.connectLogger(logjs.logger('access'), {level:'info', format:':method :url'})


app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*"); 
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  // res.header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');
  next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'html')))

// index.html//默认跳转主页
app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});


/***********************************************************************

      公共接口

************************************************************************/
// 钱包列表
app.get('/property/bcoinlist', routes.property.bcoinlist); 








/**********************************************************************

          APP接口

**********************************************************************/
//Logout
app.get('/user/logout', jwt({secret: secret.secretToken}), routes.Ausers.logout); 

//用户登录
app.post('/user/signin', routes.Ausers.signin); 

//用户短信
app.post('/user/send', routes.Ausers.send); 

//创建新用户
app.post('/user/register', routes.Ausers.register); 

//修改密码/忘记密码
app.post('/user/forget', routes.Ausers.forget); 

//修改用户信息
app.post('/user/setting', jwt({secret: secret.secretToken}), tokenManager.verifyToken, routes.Ausers.setting); 

//获取用户信息
app.get('/user/me', jwt({secret: secret.secretToken}), tokenManager.verifyToken, routes.Ausers.me); 

// 首页活动列表
app.get('/act/appActlist', routes.Aactivity.appActlist); 

// 活动详情
app.get('/act/actDetail', routes.Aactivity.actdetail); 

// 活动列表
app.post('/act/actlist', routes.Aactivity.actlist); 

// 生成充值订单
app.post('/balance/recharge', jwt({secret: secret.secretToken}), tokenManager.verifyToken, routes.Abalance.recharge); 

// 生成提现订单
app.post('/balance/tixian', jwt({secret: secret.secretToken}), tokenManager.verifyToken, routes.Abalance.tixian); 

// 对账单列表
app.get('/balance/list', jwt({secret: secret.secretToken}), tokenManager.verifyToken, routes.Abalance.list); 

// 读取钱包
app.get('/me/wallet', jwt({secret: secret.secretToken}), tokenManager.verifyToken, routes.Ame.wallet)









/**********************************************************************

          后台服务器接口

**********************************************************************/
// 用户列表
app.post('/buser/userlist',              routes.Busers.userlist);

// 新增活动
app.post('/activity/addNewActivity',    routes.Bactivity.addNewActivity);

// 活动列表
app.post('/activity/backActlist',       routes.Bactivity.backActlist);

// 修改活动
app.post('/activity/updateActivity',    routes.Bactivity.updateActivity)

// 设置开奖
app.post('/activity/reward',            routes.Bactivity.reward)

// 提现列表
app.post('/bbalance/tixianlist',        routes.Bbalance.tixianlist)

// 充值列表
app.post('/bbalance/chonglist',         routes.Bbalance.chonglist)

// 确认充值
app.post('/bbalance/confirmChong',      routes.Bbalance.confirmChong)

// 关闭充值异常
app.post('/bbalance/excpChong',         routes.Bbalance.excpChong)

// 确认提现
app.post('/bbalance/confirmTixian',      routes.Bbalance.confirmTixian)

// 校验提现
app.post('/bbalance/validate',         routes.Bbalance.validate)







process.on('uncaughtException', function(err){
  console.log(err);
})

